﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMP_QuanLySV.DAL;
using SMP_QuanLySV.BLL;

namespace SMP_QuanLySV.Presentation
{
    public partial class UC_Khoa : UserControl
    {
         Form parent;
        BLLKhoa BLL = new BLLKhoa();
        int selectedIndex = -1;
        Khoa selectedInstance = null;
        string trangThai = "";

        // constructor
        public UC_Khoa(Form parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        void setDefaultSelectedInstance()
        {
            selectedIndex = 0;
            selectedInstance = BLL.retrieveByIndex(selectedIndex);
            // update du lieu cho phu hop voi selected row
            updateTextboxes(selectedInstance);
        }

        // update cac textboxes cho phu hop voi selected row trong datagridview
        void updateTextboxes(Khoa temp)
        {
            txtKhoaID.Text = temp.KhoaID;
            txtTenKhoa.Text = temp.TenKhoa;
        }

        // set thuoc tinh readOnly/enabled cho tat ca cac controls ve 1 gia tri
        void setReadOnlyAll(bool val)
        {
            txtKhoaID.ReadOnly = val;
            txtTenKhoa.ReadOnly = val;
        }

        // cho thuoc tinh text cua cac textboxes tro thanh ""
        private void clearTextboxes()
        {
            txtKhoaID.Text= "";
            txtTenKhoa.Text = "";
        }

        // xy ly su kien click chon 1 cell khac trong datagridview
        // --> lay row cua cell do lam selectedIndex va SelectedInstance bang row dc chon
        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedIndex = dataGridViewX1.SelectedCells[0].RowIndex;
            selectedInstance = BLL.retrieveByIndex(selectedIndex);
            updateTextboxes(selectedInstance);
        }

        void setBtnEnabled1(bool val)
        {
            btnThem.Enabled = val;
            btnXoa.Enabled = val;
            btnSua.Enabled = val;
        }

        void setBtnEnabled2(bool val)
        {
            btnLuu.Enabled = val;
            btnHuy.Enabled = val;
        }



        private void UC_Khoa_Load(object sender, EventArgs e)
        {
            // nap du lieu cho datagridview
            dataGridViewX1.DataSource = BLL.returnListKhoa();

            // mac dinh dong dau tien trong datagridview duoc chon (selected)
            setDefaultSelectedInstance();

            setReadOnlyAll(true);

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            clearTextboxes();
            setReadOnlyAll(false);
            setBtnEnabled2(true);
            setBtnEnabled1(false);

            // set trang thai ve che do them moi
            trangThai = "dang_them";
            dataGridViewX1.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtKhoaID.Text == "")
            {
                MessageBox.Show("Bạn phải chọn Khoa nào cần xóa", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DialogResult result = MessageBox.Show("Bạn có chắc muốn xóa Khoa này không?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (result == DialogResult.Yes)
                {
                    // xoa doi tuong va update lai view
                    string KhoaIDCanDelete = txtKhoaID.Text;
                    Khoa KhoaCanDelete = BLL.retrieveByID(KhoaIDCanDelete);
                    BLL.deleteKhoa(KhoaCanDelete);

                    dataGridViewX1.DataSource = BLL.returnListKhoa();
                    setDefaultSelectedInstance();
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            setReadOnlyAll(false);
            txtKhoaID.ReadOnly = true;

            // set trang thai ve che do sua
            trangThai = "dang_sua";
            setBtnEnabled1(false);
            setBtnEnabled2(true);
            dataGridViewX1.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            // disable toan bo cac textboxes
            setReadOnlyAll(true);

            // kiem tra tinh hop le cua du lieu nhap vao
            Khoa tg = new Khoa();
            tg.KhoaID = txtKhoaID.Text;
            tg.TenKhoa = txtTenKhoa.Text;
          
            if (BLL.kiemTra(tg))
            {
                // update csdl va view
                if (trangThai == "dang_sua")
                {
                    BLL.updateKhoa(tg);
                }
                if (trangThai == "dang_them")
                {
                    BLL.insertKhoa(tg);
                }
                dataGridViewX1.DataSource = BLL.returnListKhoa();
                dataGridViewX1.Enabled = true;
                // disable cac nut Luu va Huy
                setBtnEnabled2(false);
                setBtnEnabled1(true);
                setDefaultSelectedInstance();
            }
            else
            {
                MessageBox.Show("Bạn nhập sai thông tin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnHuy_Click(sender, e);
            }

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            // update lai view va buttons
            setDefaultSelectedInstance();
            setReadOnlyAll(true);
            dataGridViewX1.DataSource = BLL.returnListKhoa();
            setBtnEnabled2(false);
            setBtnEnabled1(true);

            dataGridViewX1.Enabled = true;
        }

    }
}
